package com.pscsoft.lucene.DIY;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;

public class GroupCollectorTest {
    public static void main(String[] args) throws IOException {
        String indexDir = "data/store_path";
        Directory directory = FSDirectory.open(Paths.get(indexDir));
        IndexReader reader = DirectoryReader.open(directory);
        IndexSearcher searcher = new IndexSearcher(reader);
        TermQuery termQuery = new TermQuery(new Term("value", "lucene"));
       GroupCollector collector = new GroupCollector("value");
        searcher.search(termQuery, collector);
        List<ScoreDoc> docs = collector.getScoreDocs();
        for (ScoreDoc scoreDoc : docs) {
            int docID = scoreDoc.doc;
            Document document = searcher.doc(docID);
            String title = document.get("value");
            float score = scoreDoc.score;
            System.out.println(docID + ":" + title + "  " + score);
        }

        reader.close();
        directory.close();
    }
}